<template>
	<view class="balance">
		<u-navbar title="充值" :title-size="35" :title-bold="true" :immersive="true" :background="background"
			:border-bottom="false"></u-navbar>
		<view class="item" :style="{backgroundImage: 'url('+image+')',backgroundSize: '100% 100%'}">
			<view class="account">
				账户余额（元）
			</view>
			<view class="money">
				<view class="money-p" style="font-size: 30rpx;display: inline;">￥</view>{{balance?balance:0}}
			</view>
		</view>
		<!-- <view class="location" @click="show = true">
			<view class="location-p">
				<image src="https://gongxiangtaiqiu.axa2.com/taiqiu/icon/location.png"
					style="width: 36rpx; height: 40rpx; margin-right: 10rpx;"></image>
				{{near_shop_name?near_shop_name:'请选择店铺'}}
			</view>
			<view class="store">
				切换门店<image src="https://gongxiangtaiqiu.axa2.com/taiqiu/icon/cut.png"
					style="width: 24rpx; height: 22rpx; margin-left: 10rpx;">
				</image>
			</view>
		</view> -->
		<view class="title">
			<view style="width: 5rpx; height: 35rpx;background-color: #3EE69D;margin-right: 10rpx;"></view>充值金额
		</view>

		<view class="box">
			<scroll-view scroll-x class="navscroll">
				<view class="shell">
					<view class="box-item" v-for="(item,index) in discountslist" :class="{'active':current==index}"
						@click="selectvip(item,index)">
						<view class="amount">{{item.f_charge_money}}元</view>
						<!-- <view style="display: flex;">
						<view class="amount">
							{{item.f_charge_money}}
						</view>
						<view class="yuan">元</view>
					</view> -->
						<view class="give">
							<view class="give-p">
								赠送{{item.f_gift_money}}元
							</view>
						</view>
						<view class="qwe">
						</view>
					</view>
				</view>
			</scroll-view>
			<!-- <view class="btn" @click="pay">
				<view class="btn-p">立即购买</view>
			</view> -->
		</view>
		<view class="btn" @click="pay">
			<view class="btn-p">立即购买</view>
		</view>
		<u-select v-model="show" :list="shopList" @confirm="confirm"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				image: 'https://gongxiangtaiqiu.axa2.com/taiqiu/balance.png',
				current: 0,
				price: '',
				balance: '',
				discountslist: {},
				shopList: {},
				show: false,
				shop_name: '',
				near_shop_name: '',
				near_shop_id: '',
				nearlon: '',
				nearlat: '',
				options_id: '',
				options_name: '',
			}
		},

		onLoad(options) {
			// if(!uni.getStorageSync('phoneNumber')){
			// 	uni.showToast({
			// 		title: '请先手动授权手机号',
			// 		icon: "none",
			// 		duration:5000
			// 	})
			// 	setTimeout(()=>{
			// 		uni.switchTab({
			// 			url:'/pages/main/main'
			// 		})
			// 	},3000)
			// 	return

			// }
			// uni.showToast({
			// 	icon: 'none',
			// 	title: '请先选择店铺,再选择充值金额',
			// 	duration: 3500
			// })
			console.log(options, 'options是啥')
			this.options_id = options.id
			this.options_name = options.name
			this.getshoplist()
			//优惠券
			this.discounts()
			//余额
			this.blance()
			this.Nearshop()
		},
		methods: {
			Nearshop() {
				let _this = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						_this.$Api.getNearShop({
							lon: res.longitude,
							lat: res.latitude,
						}).then(res => {
							_this.near_shop_name = res.data.data.re.f_shop_name
							_this.near_shop_id = res.data.data.re.id
							uni.setStorageSync('near_id', _this.near_shop_id);
							uni.setStorageSync('near_name', _this.near_shop_name);
							_this.discounts()
							console.log(_this.near_shop_name, _this.near_shop_id, '最近商家')

						}, err => {})
					}
				});


			},
			confirm(e) {
				this.near_shop_name = e[0].label
				this.near_shop_id = e[0].value
				uni.setStorageSync('near_id', this.near_shop_id);
				uni.setStorageSync('near_name', this.near_shop_name);
				// uni.setStorageSync('near_id', this.shop_id?this.shop_id:this.near_shop_id);
				// uni.setStorageSync('near_name', this.shop_name?this.shop_name:this.near_shop_name);
				this.blance()
				this.discounts()
			},
			//获取商家列表
			getshoplist() {
				this.$Api.shopList({
					type: 3,
					page: 1,
					pageNum: 15,
					lon: uni.getStorageSync('longitude'),
					lat: uni.getStorageSync('latitude'),
				}).then(res => {
					// this.shopList = res.data.data
					let arr = res.data.data
					this.shopList = arr.map(item => {
						return {
							label: item.f_shop_name,
							value: item.id
						}
					})
					console.log(this.shopList)

				})
			},
			//余额
			blance() {
				this.$Api.getBlance({
					userId: uni.getStorageSync('userid'),
					shopId: this.options_id,
					// shopId: uni.getStorageSync('near_id'),
				}).then(res => {
					this.balance = res.data.data
					console.log(res.data.data, '成功')
				}, err => {})
			},
			//充值金额选择
			discounts() {
				this.$Api.getMeal({
					// shopId: uni.getStorageSync('near_id'),
					shopId: this.options_id,
				}).then(res => {
					this.discountslist = res.data.data
					//钱数初始化
					console.log(res, '充值金额选择列表')
					this.price = this.discountslist.length != 0 ? this.discountslist[0].f_charge_money : ''

				}, err => {})
			},
			pay() {
				let that = this
				// if (that.near_shop_name == '') {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请先选择店铺,再选择充值金额',
				// 		duration: 3500
				// 	})
				// 	that.discounts()
				// 	return
				// }

				uni.showModal({
					title: '提示',
					content: `您确认在${that.options_name} 充值吗？`,
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							that.$Api.recharge({
								money: that.price,
								userId: uni.getStorageSync('userid'),
								// shopId: uni.getStorageSync('near_id')
								shopId: that.options_id,
							}).then(res => {
								uni.requestPayment({
									provider: 'wxpay', //支付类型-固定值
									timeStamp: res.data.data.timeStamp.toString(), // 时间戳（单位：秒）
									nonceStr: res.data.data.nonceStr, // 随机字符串
									package: res.data.data.package, // 固定值
									signType: res.data.data.signType, //固定值
									paySign: res.data.data.sign, //签名
									success: function(res) {
										// console.log('success:' + JSON.stringify(res));
										console.log("支付成功");
										uni.showToast({
											icon: 'success',
											title: '支付成功'
										})
										that.blance()
									},
									fail: function(err) {
										console.log('fail:' + JSON.stringify(err));
										console.log("支付失败");
										uni.showToast({
											icon: 'none',
											title: '支付失败'
										})
									}
								});

							}, err => {})

							that.getshoplist()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			recharge() {
				uni.navigateTo({
					url: '/pagesB/rechargeAmount/rechargeAmount'
				})
			},
			//选择充值金额
			selectvip(item, index) {
				this.price = item.f_charge_money
				console.log('price', this.price, 'current', index)
				this.current = index
			},

		}
	}
</script>

<style lang="scss">
	page {
		background: #ffffff;
	}

	.location {
		width: 96%;
		height: 90rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		// padding-top: 40rpx;
		margin-left: 25rpx;

		.location-p {
			// width: 220rpx;
			// height: 50rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #222222;
			display: flex;

		}

		.store {
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #222222;
			width: 160rpx;
		}

	}

	.active {
		border: 1px solid #65E8B1;

		.amount {
			color: #000000 !important;
		}

		.give-p {
			color: #000000 !important;
		}

		.qwe {
			width: 54rpx;
			height: 54rpx;
			position: absolute;
			bottom: -14px;
			background-image: url('https://gongxiangtaiqiu.axa2.com/taiqiu/select.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
		}
	}

	.navscroll {
		white-space: nowrap; // 设置内容不换行
		width: 369px;
		height: 310rpx;
		background: #F7F8FA; // 设置滑动栏的颜色

		// 通过渗透来消除状态栏下方的下划线
		/deep/ ::-webkit-scrollbar {
			width: 4px !important;
			height: 1px !important;
			overflow: auto !important;
			background: transparent !important;
			-webkit-appearance: auto !important;
			display: block;
		}


	}

	.activepp {
		color: #222222;
	}

	.btn {
		width: 630rpx;
		height: 88rpx;
		background: #222222;
		border-radius: 44rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin: auto;
		margin-top: 20px;

		.btn-p {

			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #02FF83;
		}
	}

	.box {
		width: 690rpx;
		// height: 500rpx;
		background: #F5F5F5;
		border-radius: 16rpx;
		margin: auto;
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-direction: column;

		.shell {
			width: 350px;
			height: 310rpx;
			display: flex;
			// justify-content: space-between;
			align-items: center;
			margin: auto;
			overflow-x: scroll;
			// width: 90%;
			// height: 290rpx;
			// display: flex;
			// justify-content: space-between;
			// align-items: center;
			// margin: auto;
			// overflow-x: auto;

			.box-item {
				width: 196rpx;
				height: 268rpx;
				background: #FFFFFF;
				border-radius: 32rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				position: relative;
				margin-left: 10px;
				padding: 10px;

				.amount {
					// width: 105rpx;
					height: 80rpx;
					font-size: 40rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #7E7E7E;
					display: inline;
					margin: 20rpx;
				}

				.yuan {
					width: 24rpx;
					height: 23rpx;
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #7E7E7E;
					display: inline;
					margin-top: 37rpx;
					margin-left: 10rpx;
				}

				.give {
					// width: 126rpx;
					height: 58rpx;
					background: #F2F2F2;
					border-radius: 16rpx;
					display: flex;
					justify-content: center;
					align-items: center;

					.give-p {
						font-size: 22rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #7E7E7E;
						padding: 20rpx;
					}
				}
			}
		}
	}

	.balance {
		width: 100%;
		// height: auto;
	}

	.title {
		width: 140rpx;
		height: 89rpx;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #222222;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-left: 40rpx;
	}

	.item {
		width: 691rpx;
		height: 300rpx;
		margin: auto;
		margin-top: 200rpx;
		border-radius: 20px;

		// background-image:url('https://static.runoob.com/images/mix/gradient2.png');
		.account {
			width: 250rpx;
			height: 29rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			padding-top: 60rpx;
			margin-left: 60rpx;
		}

		.money {
			width: 231rpx;
			height: 48rpx;
			font-size: 30px;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			margin-top: 50rpx;
			margin-left: 60rpx;
		}

		.topup {
			width: 148rpx;
			height: 56rpx;
			background: #222222;
			border-radius: 28rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-left: 500rpx;

			.topup-p {
				width: 55rpx;
				// height: 28rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #02FF83;


			}
		}
	}
</style>